package me.ingxin.android.sample.lego.interceptor

import android.content.Context
import androidx.appcompat.app.AlertDialog
import me.ingxin.android.router.annotaions.Interceptor
import me.ingxin.android.router.route.RouteInterceptor
import me.ingxin.android.router.route.Request
import me.ingxin.android.sample.lego.utils.logD

/**
 * Created by ingxin on 2022/4/27
 */
@Interceptor("appGlobal4", global = true, priority = 5)
class Global4Interceptor : RouteInterceptor {
    override fun handle(context: Context, request: Request, dispatcher: RouteInterceptor.Dispatcher) {
        "Global4Interceptor开始执行".logD()
        if (request.uri.path == "/activity/test3") {
            //通过路由地址和携带的参数判断是否需要拦截，根据业务修改
            AlertDialog.Builder(context)
                .setTitle("是否重定向？")
                .setMessage("Global4Interceptor拦截到${request.uri.path}，\n是否需要重定向到/activity/test1")
                .setPositiveButton("是") { _, _ ->
                    request.setRedirect("/activity/test1")
                        .putString("c", "我是通过重定向传递的")
                    //继续执行
                    dispatcher.continueRun()
                }
                .setNegativeButton("否") { _, _ ->
                    //继续执行
                    dispatcher.continueRun()
                }
                .create()
                .show()
        } else {
            //继续执行
            dispatcher.continueRun()
        }
    }
}